//
//  HCButton.m
//  CustomButton
//
//  Created by leeson on 2018/2/5.
//  Copyright © 2018年 李斯芃 ---> 512523045@qq.com. All rights reserved.
//

#import "HCButton.h"

@implementation HCButton

//MARK: - --- 系统初始化方法
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
        self.space = 0;
    }
    return self;
}

//MARK: - --- 自定义初始化方法
- (instancetype)initWithFrame:(CGRect)frame withImage:(NSString *)imge andTitle:(NSString *)title andSpace:(CGFloat)space{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
        [self setImage:[UIImage imageNamed:imge] forState:UIControlStateNormal];
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.space = space;
    }
    return self;
}

//MARK: - --- 自定义初始化方法(传buttonType)
+ (HCButton*)buttonWithType:(UIButtonType)buttonType andFrame:(CGRect)frame andImage:(NSString *)imge andTitle:(NSString *)title andSpace:(CGFloat)space{
    HCButton *bt = [HCButton buttonWithType:buttonType];
    bt.frame = frame;
    [bt setImage:[UIImage imageNamed:imge] forState:UIControlStateNormal];
    [bt setTitle:title forState:UIControlStateNormal];
    [bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    bt.space = space;
    return bt;
}

//MARK: - --- xib初始化方法
- (void)awakeFromNib{
    [super awakeFromNib];
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    self.space = 0;
}

//MARK: - --- 设置上下间距开始计算相对位置
- (void)setSpace:(CGFloat)space{
    _space = space;
    [self moveSubViewsWithFrame:self.frame];
}

//MARK: - --- 计算相对位置
- (void)moveSubViewsWithFrame:(CGRect)frame{
    CGFloat space = 0;
    if (self.space != 0) {
        space = self.space;
    }
    //1.使imageView居中
    //获取imageView中的宽度和高度
    CGFloat imageW = self.imageView.frame.size.width;
    CGFloat imageH = self.imageView.frame.size.height;
    //获取titleLabel的宽度
    CGFloat labelW = self.titleLabel.frame.size.width;
    CGFloat labelH = self.titleLabel.frame.size.height;
    //计算移动距离
    CGFloat imageDistance = (self.frame.size.width - imageW) * 0.5;
    //设置上内距
    CGFloat imageTop = (self.frame.size.height - imageH - labelH - space) * 0.5;
    //进行移动
    self.imageEdgeInsets = UIEdgeInsetsMake(imageTop, imageDistance, 0, 0);
    //NSLog(@"移动后的imageView%@",NSStringFromCGRect(self.imageView.frame));
    
    //使titleLabel居于imageView的正下方且居中
    //计算移动距离
    CGFloat labelDistance = - imageW + (self.frame.size.width - labelW) *0.5 ;
    //上内距
    CGFloat labelTop = imageTop + imageH + space;
    self.titleEdgeInsets =UIEdgeInsetsMake(labelTop, labelDistance,0,0);
    //NSLog(@"移动后的titleLabel%@",NSStringFromCGRect(self.titleLabel.frame));
}


@end
